/**
 * 维护页面JS
 * User: zhaoming
 * Date: 15-8-17
 * Time: 下午5:58
 * To change this template use File | Settings | File Templates.
 */
$(function(){
    //激活左侧导航菜单
    activeNavBar("MANAGE_OPT_ORG_INFO");
    initSelect2();
    
    $("#saveBtn").click(function(){
        save();
    });
    $("#goBackBtn, #headGoBackBtn").click(function(){
        window.forward(springUrl+ "/web/orgInfo/index");
    });
    initData();
});

function initData(){
    jQuery.ajax({
        type: "POST",
        dataType: "json",
        url: springUrl+"/web/orgInfo/detail",
        data: {
            uniqueNo: $('#uniqueNo').val()
        },
        success: function (data) {
            $("#orgName").val(data.orgName);
            $("#linkman").val(data.linkman);
            $("#mobileNo").val(data.mobileNo);
            $("#address").val(data.address);
            $("#remark").val(data.remark);

            var param = [
                {
                    id: 'provinceCode',
                    url: springUrl+ '/web/orgInfo/listDistrictInfo',
                    code: 'districtCode',
                    name: 'districtName',
                    parent: '',
                    value: data.provinceCode
                },{
                    id: 'cityCode',
                    url: springUrl+ '/web/orgInfo/listDistrictInfo',
                    code: 'districtCode',
                    name: 'districtName',
                    parent: 'parentDistrictCode',
                    value: data.cityCode
                },{
                    id: 'countyCode',
                    url: springUrl+ '/web/orgInfo/listDistrictInfo',
                    code: 'districtCode',
                    name: 'districtName',
                    parent: 'parentDistrictCode',
                    value: data.countyCode
                }
            ];
            new Linkage().init(param);
        },
        error: function(data){
            Alert.error("初始化数据失败!");
        }
    });
}

/**
 * 保存
 */
function save(){
    if($('#editForm').trigger("validate").isValid()){
        var index =  layer.msg('正在处理中...', {icon: 16, shade: 0.3, time: 0});
        jQuery.ajax({
            type: "POST",
            dataType: "json",
            url: springUrl+"/web/orgInfo/update",
            data: {
                uniqueNo: $("#uniqueNo").val(),
                orgName: $('#orgName').val(),
                linkman: $('#linkman').val(),
                mobileNo: $('#mobileNo').val(),
                provinceCode: $('#provinceCode').val(),
                provinceName: $('#provinceCode').val()!="" ? $('#provinceCode option:selected').text() : "",
                cityCode: $('#cityCode').val(),
                cityName: $('#cityCode').val()!="" ? $('#cityCode option:selected').text() : "",
                countyCode: $('#countyCode').val(),
                countyName: $('#countyCode').val()!="" ? $('#countyCode option:selected').text() : "",
                address: $('#address').val(),
                remark: $('#remark').val()
            },
            success: function (data) {
                layer.close(index);
                Alert.success("数据保存成功!", function(){
                    window.forward(springUrl+ "/web/orgInfo/index");
                });
            },
            error: function(data){
                layer.close(index);
                Alert.error(data.responseText);
            }
        });
    }
}

